//
//  JMPickerDate.m
//  NbTest
//
//  Created by admin on 12-3-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JMPickerDate.h"
#import <objc/message.h>
#import "JMEnv.h"
#import "AppDelegate.h"

#define isIOS7 ( [[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7 )
#define isIOS8 ( [[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=8 )
static JMPickerDate* _Instance_JMPickerDate ;

@implementation JMPickerDate
@synthesize _YYSelString, _tableView, _pickerDate ,_actionSheet, _titleLabel;


+ (id) getInstance{
    //static JMPickerDate* _Instance_JMPickerDate ;
    
    if (! _Instance_JMPickerDate)
    {
        _Instance_JMPickerDate = [[JMPickerDate alloc]init];
    }
    
    return _Instance_JMPickerDate;
}


+ (void) delocateInstance
{
    //static JMPickerDate* _Instance_JMPickerDate ;
    
    if (! _Instance_JMPickerDate)
    {
        [_Instance_JMPickerDate dealloc];
        _Instance_JMPickerDate = nil;
    }
    
}


- (id) init
{
    if (self = [super init])
    {
        if (isIOS8) {
            _sheetVC = [[SheetViewController alloc] init];
        }else{
            _actionSheet=[[UIActionSheet alloc]initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle: nil otherButtonTitles:nil, nil];
            //确定、取消按钮
            UIBarButtonItem *_buttonItemFlexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            UIBarButtonItem *_buttonItemRight = [[UIBarButtonItem alloc] initWithTitle:@"确定"
                                                                             style: UIBarButtonItemStyleBordered
                                                                            target:self
                                                                            action:@selector(selectDateAffirm)];
        
            UIBarButtonItem *_buttonItemLeft  = [[UIBarButtonItem alloc] initWithTitle:@"取消"
                                                                             style:UIBarButtonItemStyleBordered 
                                                                            target:self 
                                                                            action:@selector(selectDateCanceled)];
        
            UIToolbar *_tool=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
            _tool.barStyle=UIBarStyleDefault;
            if (isIOS7)
            {
                _tool.frame = CGRectMake(8, 0, 304, 44) ;
                [_tool setBarTintColor:ENV.iToolBarColor];
                _tool.barStyle=UIBarStyleBlackOpaque;
                [_tool setTintColor:[UIColor whiteColor]];
            }
            _tool.backgroundColor=[UIColor clearColor];
            NSArray *_array = [[NSArray alloc] initWithObjects:_buttonItemLeft,_buttonItemFlexible,_buttonItemRight,nil];
            [_tool setItems: _array];
            [_buttonItemLeft  release];
            [_buttonItemFlexible release];
            [_buttonItemRight release];
            [_array release];
        
        
            [_actionSheet addSubview:_tool];
            [_tool release];
            //选择日期
            _pickerDate = [[UIDatePicker alloc]init];
            _pickerDate.frame=CGRectMake(0, 44, 320, 217);
            _pickerDate.datePickerMode=UIDatePickerModeDate;
   
        
            UILabel*   label =  [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
            _titleLabel = label;
            //[label release];//在这里release会报错
            _titleLabel.text = @" title ";
            _titleLabel.textColor = [UIColor whiteColor];
            _titleLabel.font = [UIFont boldSystemFontOfSize:19];
            _titleLabel.textAlignment = UITextAlignmentCenter;
            _titleLabel.backgroundColor = [UIColor clearColor];
            [_actionSheet addSubview:_titleLabel];
            [label release];
            [_actionSheet addSubview:_pickerDate];
        }
        
    }
    
    return self;
}


+(void)PopViewWithYYString:(NSMutableString*)aString andIn:(UITableView*) aView andTitle:(NSString*) aTitle;
{
    [JMPickerDate PopViewWithYYString:aString andIn:aView andTitle: aTitle with:nil];
}
+(void)PopViewWithYYString:(NSMutableString*)aString andIn:(UITableView*) aView andTitle:(NSString*) aTitle with:(id<JMPickerDateDelegate>) aDelegate
{
    JMPickerDate* instance  = [JMPickerDate getInstance];
    if (isIOS8) {
        instance.sheetVC.dateDelegate = aDelegate;
        instance.sheetVC.YYSelString = aString;
        instance.sheetVC.titleText = aTitle;
        instance.sheetVC.table = aView;
        instance.sheetVC.pickType = JMPickerDatePicker;
        
        //设置时间
        if (aString!=nil && aString.length != 0)
        {
            NSDateFormatter* format = [[NSDateFormatter alloc]init];
            [format setDateFormat:@"yyyy-MM-dd"];
            NSDate* curDate = [format dateFromString:aString];
            [format release];
            [instance.sheetVC.datePickView setDate:curDate];
        }
        
         AppDelegate*delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
        instance.sheetVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
#endif
        UIViewController* presentingVC = delegate.window.rootViewController;
        if (presentingVC.presentedViewController) {
            presentingVC = presentingVC.presentedViewController;
        }
        [presentingVC presentViewController:instance.sheetVC animated:YES completion:^(void){
            //instance.sheetVC.titleLabel.text = aTitle;
        }];
    
    }else{
        
        instance._titleLabel.text = aTitle;
        instance._YYSelString = aString;
        instance._tableView = aView;
        instance.iDelegate = aDelegate;
    
        //设置时间
        if (aString!=nil && aString.length != 0)
        {
            NSDateFormatter* format = [[NSDateFormatter alloc]init];
            [format setDateFormat:@"yyyy-MM-dd"];
            NSDate* curDate = [format dateFromString:aString];
            [format release];
            [instance._pickerDate setDate:curDate];
        }
    
        [instance._actionSheet showInView:instance._tableView];
    }
}

#pragma mark----------日期确定
-(void)selectDateAffirm
{
    
    NSDate* date = [_pickerDate date];     
    
    
    NSDateFormatter* format = [[NSDateFormatter alloc]init];
	[format setDateFormat:@"yyyy-MM-dd"];
    NSString* substring = [format stringFromDate:date];
    
    
    [_YYSelString setString:substring];
    
    
    
    
    [_actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
    [format release];
    
    
    if (self.iDelegate)
    {
        [self.iDelegate didSeletedDateStr:self._YYSelString];
        self.iDelegate = nil;
    }
    
    _YYSelString = nil;
    
    //
    if ([self._tableView isKindOfClass:[UITableView class]])
    {
         [_tableView reloadData];
    }
    
}

#pragma mark----------日期取消
-(void)selectDateCanceled
{
      _YYSelString = nil;
    [_actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
    //objc_msgSend(self,@selector(close:));
}

- (void) dealloc
{
    _titleLabel = nil;
    _pickerDate = nil;
    _actionSheet = nil;
    _YYSelString = nil;
    _tableView = nil;
    [super dealloc];
}

#pragma  mark - instance
+ (id)allocWithZone:(NSZone *)zone
{
    if (_Instance_JMPickerDate == nil) 
    {
        _Instance_JMPickerDate = [super allocWithZone:zone];
        return _Instance_JMPickerDate;  // assignment and return on first allocation
    }
    
    return nil;
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (unsigned)retainCount
{
    return UINT_MAX;  //denotes an object that cannot be released
}

- (oneway void)release
{
    
    //do nothing
}

- (id)autorelease
{
    return self;
}

#pragma -mark 其他
//不属于这个类的方法 暂时放这里 用于获得前一个月的日期
+(NSDate *)getPriousDateFromDate:(NSDate *)date withMonth:(int)month
{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    [comps release];
    [calender release];
    return mDate;
}

+(NSString *)getStrPriousDateFromDate:(NSDate *)date withMonth:(int)month
{
//    NSDateComponents *comps = [[NSDateComponents alloc] init];
//    [comps setMonth:month];
//    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
//    [comps release];
//    [calender release];
////    return mDate;
    NSDate* mDate = [self getPriousDateFromDate:date withMonth: month];
    
    NSDateFormatter* format = [[NSDateFormatter alloc]init];
	[format setDateFormat:@"yyyy-MM-dd"];
    NSString* dateString = [format stringFromDate:mDate];
    [format release];
    return dateString;
    
}

+(NSString *)getStrPriousDateFromStr:(NSString *)strDate withMonth:(int)month
{
    NSDateFormatter* format = [[NSDateFormatter alloc]init];
	[format setDateFormat:@"yyyy-MM-dd"];
    NSDate* date = [format dateFromString:strDate];
    [format release];
    
    return [self getStrPriousDateFromDate:date withMonth: month];
}
@end
